#pragma once

#include <map>
#include "types.h"

class CElement;
class CWnd;

class CSquarePane
{
public:
    CSquarePane(CWnd* pWnd, UINT nRowNum, UINT nColNum);
    ~CSquarePane();

    /*
     * It will first draw background, then square line, and then element
     */
    void Draw();

    void Restart();

    /*
     * Get next element
     */
    ElementAttr GetNextElementAttr();

    /*
     * Construct element with ElementAttr poped from queue 
     */
    CElement* UpdateElement();

    /*
     * Construct element with ElementAttr poped from queue 
     */
    CElement* ConstructElement(const CPoint& pntStart, const ElementAttr& attr);

    /*
     * Merge element to square lines
     */
    void MergeElement();

    /*
     *
     */
	bool ValidateSquareList(const SquareList& listSquare);
    bool ValidateElement(const CElement* pElement);

	// Attributes
	CWnd*   GetPaneWnd() const;
	UINT    GetSquareWidth() const;
    UINT    GetSqaureRowNum() const;
    UINT    GetSquareColNum() const;
    CRect   GetPaneRect() const;
    CSquare GetSquare(UINT nRow, UINT nCol);
	UINT    GetScore() const;

    // Convert
    CPoint      ConvertPositionToPoint(const Position& pos) const;
    Position    ConvertPointToPosition(const CPoint& pnt) const;
	
private:
    void Init();
    bool SquareInPane(const CSquare& square);
	bool SquareFlagged(const CSquare& square);
    void DrawBackGround(CDC* pDC);
    void UpdateSquareLine();
    void AddElementAttr(UINT nCount);
    UINT CalcScore(UINT nLineNum);

private:
    CWnd            *m_pPaneWnd;        // Window belong to
    CDC             *m_pPaneDC;
    CRect           m_rectPane;
    CRect           m_rectShow;         // Show rect used in fact

    UINT            m_nSquareWidth;
    UINT            m_nSquareRowNum;
    UINT            m_nSquareColNum;

    ElementAttrQue  m_queElementAttr;
    CPoint          m_pntElementStart;

    SquareLineVec   m_vecSquareLine;    // Static squares
    CElement        *m_pElement;        // Active squares

    UINT            m_nScore;
};